package com.css.project.oa.phoneMenu.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.css.common.utils.DateUtils;
import com.css.common.utils.SecurityUtils;
import com.css.framework.security.LoginUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.css.project.oa.phoneMenu.mapper.ProPhoneMenuMapper;
import com.css.project.oa.phoneMenu.domain.ProPhoneMenu;
import com.css.project.oa.phoneMenu.service.IProPhoneMenuService;
import com.css.project.oa.phoneMenu.vo.PhoneMenuVo;

/**
 * 手机菜单管理Service业务层处理
 * 
 * @author css
 * @date 2020-06-12
 */
@Service
public class ProPhoneMenuServiceImpl implements IProPhoneMenuService 
{
    @Autowired
    private ProPhoneMenuMapper proPhoneMenuMapper;

    /**
     * 查询手机菜单管理
     * 
     * @param id 手机菜单管理ID
     * @return 手机菜单管理
     */
    @Override
    public ProPhoneMenu selectProPhoneMenuById(Long id)
    {
        return proPhoneMenuMapper.selectProPhoneMenuById(id);
    }

    /**
     * 查询手机菜单管理列表
     * 
     * @param proPhoneMenu 手机菜单管理
     * @return 手机菜单管理
     */
    @Override
    public List<ProPhoneMenu> selectProPhoneMenuList(ProPhoneMenu proPhoneMenu)
    {
        return proPhoneMenuMapper.selectProPhoneMenuList(proPhoneMenu);
    }

    /**
     * 新增手机菜单管理
     * 
     * @param proPhoneMenu 手机菜单管理
     * @return 结果
     */
    @Override
    public int insertProPhoneMenu(ProPhoneMenu proPhoneMenu)
    {
    	LoginUser cu = SecurityUtils.getLoginUser();
    	proPhoneMenu.setCreateTime(new Date());
    	proPhoneMenu.setCreateBy(cu.getUsername());
        proPhoneMenu.setUpdateTime(new Date());
        proPhoneMenu.setUpdateBy(cu.getUsername());
        proPhoneMenu.setDelFlag("0");
        return proPhoneMenuMapper.insertProPhoneMenu(proPhoneMenu);
    }

    /**
     * 修改手机菜单管理
     * 
     * @param proPhoneMenu 手机菜单管理
     * @return 结果
     */
    @Override
    public int updateProPhoneMenu(ProPhoneMenu proPhoneMenu)
    {
        proPhoneMenu.setUpdateTime(DateUtils.getNowDate());
        return proPhoneMenuMapper.updateProPhoneMenu(proPhoneMenu);
    }

    /**
     * 批量删除手机菜单管理
     * 
     * @param ids 需要删除的手机菜单管理ID
     * @return 结果
     */
    @Override
    public int deleteProPhoneMenuByIds(Long[] ids)
    {
    	String idStr = "";
    	for(Long id : ids) {
    		idStr += id+",";
    	}
    	List<ProPhoneMenu> childs = proPhoneMenuMapper.getChildPhoneMenu(idStr.substring(0, idStr.length()-1));
        if(childs != null) {
        	if(childs.size()>0) {
        		return -1;
        	}
        }
    	return proPhoneMenuMapper.deleteProPhoneMenuByIds(ids);
    }

    /**
     * 删除手机菜单管理信息
     * 
     * @param id 手机菜单管理ID
     * @return 结果
     */
    @Override
    public int deleteProPhoneMenuById(Long id)
    {
        return proPhoneMenuMapper.deleteProPhoneMenuById(id);
    }

	@Override
	public List<ProPhoneMenu> getMainPhoneMenu() {
		// TODO Auto-generated method stub
		return proPhoneMenuMapper.getMainPhoneMenu();
	}

	@Override
	public List<PhoneMenuVo> getChildMenuForOutNet(Long id) {
		// TODO Auto-generated method stub
		List<PhoneMenuVo> list = new ArrayList<PhoneMenuVo>();
		List<ProPhoneMenu> mainMenus = proPhoneMenuMapper.getChildPhoneMenu(id+"");
		for(int i=0;i<mainMenus.size();i++) {
			PhoneMenuVo m = new PhoneMenuVo();
			m.setMenuName(mainMenus.get(i).getMenuName());
			List<PhoneMenuVo> cList = new ArrayList<PhoneMenuVo>();
			List<ProPhoneMenu> child = proPhoneMenuMapper.getChildPhoneMenu(mainMenus.get(i).getId()+"");
			for(int j=0;j<child.size();j++) {
				PhoneMenuVo c = new PhoneMenuVo();
				c.setMenuName(child.get(j).getMenuName());
				c.setMenuPic(child.get(j).getMenuPicPath());
				c.setMenuUrl(child.get(j).getMenuUrl());
				cList.add(c);
			}
			m.setChildMenu(cList);
			list.add(m);
		}
		return list;
	}
	
}
